//implementare elemente de baza
//varianta compatibila Mozilla


function xmlBasicElements(domElement){
	/**
	* constructor
	* @param DOMDocument handlerul documentului pt care se creaza elementul
	*/
	this.dom=domElement;
	this.parentElement=this.dom.documentElement;
	this.type='';
	this.id='';
	this.content;

	/**
	* creare paragraf
	* @param String id
	* @param String content continutul noului paragraf
	*/
	//updated
	this.createParagraph=function(id, content){
		this.type='paragraf';
		this.content=content;
		this.id=id;
		newPara=this.dom.createElement('paragraph', content);
		newPara.setAttribute('paraId', id);
		newPara.appendChild(this.dom.createTextNode(content));
		newParaC=this.dom.importNode(newPara, true);
		this.parentElement.appendChild(newParaC);
		return this;
	};
	
	/**
	*	update paragraph
	*
	*/
	this.updateParagraph=function(id, content){
		this.type='paragraph';
		this.content=content;
		this.id=id;
		
		var oldParaList=this.dom.getElementsByTagName(this.type);
		var oldPara;
		for(var i=0; i<oldParaList.length; i++){
			oldParaId=oldParaList[i].getAttribute('paraId');
			if(oldParaId == this.id){
				oldPara=oldParaList[i];
			}
		}
		
		newPara=this.dom.createElement('paragraph', content);
		newPara.setAttribute('paraId', id);
		newPara.appendChild(this.dom.createTextNode(content));
		newParaC=this.dom.importNode(newPara, true);		
		
		this.parentElement.replaceChild(newParaC, oldPara);
		return this;		
	}
	
	/**
	* creare nota
	* @param String id
	* @param String content continutul noii note
	*/
	this.createNote=function(id, content){
		this.type='note';
		this.content=content;
		this.id=id;
		newNote=this.dom.createElement('note', content);
		newNote.setAttribute('noteId', id);
		newNote.appendChild(this.dom.createTextNode(content));
		newNoteC=this.dom.importNode(newNote, true);
		this.parentElement.appendChild(newNoteC);
		return this;
	};
	
	/**
	* creare citat
	* @param String id
	* @param String content continutul noului citat
	*/
	this.createQuote=function(id, content){
		this.type='quote';
		this.content=content;
		this.id=id;
		newQuote=this.dom.createElement('quote', content);
		newQuote.setAttribute('quoteId', id);
		newQuote.appendChild(this.dom.createTextNode(content));
		newQuoteC=this.dom.importNode(newQuote, true);
		this.parentElement.appendChild(newQuoteC);
		return this;
	};
	
	/**
	*	creare tabel
	*/
	this.createTable=function(id, title, headers){
		this.type='table';
		this.id=id;
	/*	this.content=new Array();
		this.content['title']=title;
		this.content['headers']=headers;*/
	 	this.content="title:"+title+";headers:";
	 	for(var i=0; i<headers.length; i++){
	 		this.content+=headers[i];
	 		if(i !== (headers.length-1)){
	 			this.content+=",";
	 		}
	 	}
	 	//this.content+=";";
		
		//console.dir(this.content);
		
		var newTable=this.dom.createElement('table');
		newTable.setAttribute('tbId',id);
		
		var tableCaption=this.dom.createElement('tableCaption');
		tableCaption.appendChild(this.dom.createTextNode(title));
		newTable.appendChild(tableCaption);
		
		var tableHeader=this.dom.createElement('tableHeader');
		var tableHeaderTr=this.dom.createElement('tableRow');
		tableHeaderTr.setAttribute('rowIndex','0');
		
		var headerLength=parseInt(headers.length);
		for(var i=0; i<headerLength; i++){
			var td=this.dom.createElement('tableColumn');
			td.setAttribute('colType',this.trimString(headers[i]));
			td.appendChild(this.dom.createTextNode(headers[i]));
			tableHeaderTr.appendChild(td);
		}
		
		tableHeader.appendChild(tableHeaderTr);
		newTable.appendChild(tableHeader);
		
		newTableC=this.dom.importNode(newTable, true);
		this.parentElement.appendChild(newTableC);
		
		//load for xhr
		/*var newTableContent=new Array();
		newTableContent['title']=title;
		newTableContent['headers']=headers;
		this.content=newTableContent;*/
		
		//console.dir(this.content);
		
		return this;		
	}

	
	//UTILITY FUNCTION	
	this.trimString=function(string){
		var strArr=string.split(" ");
		var returnString='';
		if(strArr.length > 1){
			for(var i=0; i<strArr.length; i++){
				returnString+=strArr[i].toLowerCase();
			}
		}else{
			returnString=string;
		}
		return returnString;
	}
	
}